[#include "../../../common/html.ftl" /]
[@html]
    [@head title="角色列表" css=["fw", "bootstrap-table", "bootstrap-table-editable"] /]
    [@body]
        [#--主体内容--]
        [@main ]
            <div class="fw-tables">
                [#--表格主体内容--操作栏--]
                [@mainHeader]
                    [#--[@shiro.hasPermission name="system:role:add"]--]
                    <button class="am-btn am-btn-success am-radius" onclick="$.operate.addFull()">添加</button>
                    [#--[/@shiro.hasPermission]--]
                    [#--[@shiro.hasPermission name="system:role:remove"]--]
                    <button class="am-btn am-btn-danger am-radius multiple am-disabled" onclick="$.operate.removeAll()">删除</button>
                    [#--[/@shiro.hasPermission]--]
                [/@mainHeader]

                [#--表格主体内容--搜索栏--]
                [@searchForm searchId="form-role-search" ]
                    <div class="am-form-group">
                        <input type="text" name="roleName" class="am-form-field am-input-sm am-radius" placeholder="角色名称">
                    </div>
                    <div class="am-form-group">
                        <select name="status" class="am-radius" data-am-selected placeholder="角色状态">
                            <option value=""></option>
                            [#list dict.getType("sys_role_status") as dict]
                                <option value="${dict.dictValue}">${dict.dictLabel}</option>
                            [/#list]
                        </select>
                    </div>
                    <div class="am-form-group">
                        <select name="buildIn" class="am-radius" data-am-selected placeholder="是否内置角色">
                            <option value=""></option>
                            [#list dict.getType("sys_yes_no") as dict]
                                <option value="${dict.dictValue}">${dict.dictLabel}</option>
                            [/#list]
                        </select>
                    </div>
                [/@searchForm]

                <div class="fw-table">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        [/@main]

        [#--PC/APP 右侧悬浮按钮--]
        [@rightToolbar /]

        [@script js=["fw", "bootstrap-table", "bootstrap-table-editable"]]
            <script type="text/javascript">
                [#--var editFlag = "${hasPermission('system:role:edit')}";--]
                [#--var detailFlag = "${hasPermission('system:role:detail')}";--]
                [#--var removeFlag = "${hasPermission('system:role:remove')}";--]
                [#--var authDataScopeFlag = "${hasPermission('system:role:authDataScope')}";--]
                var editFlag = "";
                var detailFlag ="";
                var removeFlag ="";
                var authDataScopeFlag ="";
                var datasYesNo = ${convert.toJson(dict.getType("sys_yes_no"))};
                var isAdministrator = ${convert.toJson(common.isAdministrator())};

                var prefix = ctx + "osfw/system/manage/role";

                $(function () {
                    var options = {
                        url: prefix + "/list",
                        createUrl: prefix + "/add",
                        updateUrl: prefix + "/edit/{id}",
                        detailUrl: prefix + "/detail/{id}",
                        removeUrl: prefix + "/remove",
                        sortName: "roleSort",
                        uniqueId: "roleId",
                        sortOrder: "asc",
                        modalName: "角色",
                        onEditableSave: onEditableSave,
                        columns: [
                            {
                                checkbox: true
                            },
                            {
                                title: "序号",
                                formatter: function (value, row, index) {
                                    return $.table.serialNumber(index);
                                }
                            },
                            {
                                field: 'roleName',
                                title: '角色名称',
                                sortable: true
                            },
                            {
                                visible: editFlag == 'hidden' ? false : true,
                                title: '角色状态',
                                align: 'left',
                                formatter: function (value, row, index) {
                                    return statusTools(row);
                                }
                            },
                            {
                                visible: isAdministrator ? true : false,
                                switchable: isAdministrator ? true : false,
                                field: 'buildIn',
                                title: '系统内置',
                                formatter: function (value, row, index) {
                                    return $.table.selectDictLabel(datasYesNo, value);
                                }
                            },
                            {
                                field: 'createTime',
                                title: '创建时间',
                                sortable: true
                            },
                            {
                                field: 'roleSort',
                                title: '显示顺序',
                                editable: isAdministrator ? {
                                    title: '排序',
                                    validate: function (v) {
                                        return validateSortNum(v);
                                    }
                                } : ''
                            },
                            {
                                title: '操作',
                                cellStyle: operCellStyle,
                                formatter: function (value, row, index) {
                                    var actions = [];
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.editFull(\'' + row.roleId + '\')">编辑</a> ');
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + removeFlag + '"  href="javascript:void(0)" onclick="$.operate.remove(\'' + row.roleId + '\')">删除</a> ');
                                    // actions.push('<a class="am-badge am-badge-transparent am-radius ' + detailFlag + '" href="javascript:void(0)" onclick="$.operate.detailFull(\'' + row.roleId + '\')">详情</a> ');
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + authDataScopeFlag + '" href="javascript:void(0)" onclick="authDataScope(\'' + row.roleId + '\')">数据权限</a> ');
                                    actions.push('<a class="am-badge am-badge-transparent am-radius ' + editFlag + '" href="javascript:void(0)" onclick="authUser(\'' + row.roleId + '\')">分配用户</a>');
                                    return actions.join('');
                                }
                            }
                        ]
                    };
                    $.table.init(options);
                });

                /** 清理参数缓存 */
                function clearCache() {
                    $.operate.get(prefix + "/clearCache");
                }

                /* 角色管理-分配数据权限 */
                function authDataScope(roleId) {
                    $.modal.open("分配数据权限", prefix + '/authDataScope/' + roleId, 450, 750);
                }

                /* 角色管理-分配用户 */
                function authUser(roleId) {
                    $.modal.openFull("分配用户", prefix + '/authUser/' + roleId, null, false);
                }

                /* 角色状态显示 */
                function statusTools(row) {
                    if (row.status == 1) {
                        return '<i class=\"am-icon-toggle-off am-text-danger am-text-lg\" onclick="enable(\'' + row.roleId + '\',\'' + row.rolePub + '\')"></i> ';
                    } else {
                        return '<i class=\"am-icon-toggle-on am-text-success am-text-lg\" onclick="disable(\'' + row.roleId + '\',\'' + row.rolePub + '\')"></i> ';
                    }
                }

                /* 角色管理-停用 */
                function disable(roleId, rolePub) {
                    if (isAdministrator || rolePub == 'N'){
                        $.modal.confirm("确认要停用角色吗？", function () {
                            $.operate.post(prefix + "/changeStatus", {"roleId": roleId, "status": 1});
                        })
                    }
                }

                /* 角色管理启用 */
                function enable(roleId, rolePub) {
                    if (isAdministrator || rolePub == 'N'){
                        $.modal.confirm("确认要启用角色吗？", function () {
                            $.operate.post(prefix + "/changeStatus", {"roleId": roleId, "status": 0});
                        })
                    }
                }

                /* 行内编辑 */
                function onEditableSave(field, row, oldValue, $el) {
                    var value = row[field];
                    var data = new FormData();
                    data.append(field, value);
                    data.append("roleId", row["roleId"]);
                    $.ajax({
                        url: prefix + "/editSaveRoleSort",
                        type: "POST",
                        dataType: "json",
                        data: data,
                        processData: false,
                        contentType: false,
                        success: function (result) {
                            $.operate.ajaxSuccess(result);
                            if (result.code == '301'){
                                $.table.refresh();
                            }
                        }
                    })
                }

                /* 校验排序数值 */
                function validateSortNum(v) {
                    if (!v) {return '排序不能为空'};
                    var reg = /^\+?[1-9][0-9]{0,2}$/;
                    if (!reg.test(v)) {
                        return '请输入 1 至 999 之间的数值';
                    }
                }

            </script>
        [/@script]
    [/@body]
[/@html]
